catDog string problem at Codingbat.com [closed]
Posted
by stanny110
on Stack Overflow
See other posts from Stack Overflow
or by stanny110
Published on 2010-03-29T19:38:25Z
Indexed on
2010/03/29
19:43 UTC
Read the original article
Hit count: 501
public boolean catDog(String str)
{ int catAnswer = 0; int dogAnswer = 0; int cat_Count = 0; int dog_Count = 0;
for (int i=0; i< str.length()-1; i++) { String sub = str.substring(i, i+2);
if ((sub.equals("cat"))) cat_Count++;
if ((sub.equals("dog"))) dog_Count++;
catAnswer = cat_Count;
dogAnswer = dog_Count;
} //end for
if(dogAnswer == catAnswer ) {return true;} // else return (dogAnswer != catAnswer) ;
© Stack Overflow or respective owner